<?php

require_once ($DOCUMENT_ROOT . '/includes/functions.php');

session_start ();

if (valid_user ())
{
	$headerInserts = '<link type="text/css" rel="StyleSheet" href="css/revised.css" />
<script type="text/javascript" src="js/bobcat.js"></script>
<script type="text/javascript" src="js/ruler.js"></script>
<link type="text/css" rel="StyleSheet" href="css/ruler.css" />
<script type="text/javascript"> window.onload=function(){tableruler();} </script>';
	do_header ('Instructors', $headerInserts);
	
	do_menu ();
	
	// resolve the current department if selected
	if ($d && $d != 0)
	{
		db_connect ();
		$query = "SELECT department FROM dlist WHERE id='$d'";
		$result = mysql_query ($query);
		$departmentName = mysql_result ($result, 0);
	}
	else
		$departmentName = "All Departments";
	echo "<h1>Instructors in $departmentName</h1>";
	?>
	<h3><a href="user_newteacher.php">add a new instructor</a></h3>

	<table class='sort-table' id='list'>
		<thead id="header">
		 <tr>
		 <td id='name' title='Sort by Name' style="padding-bottom:2px;">Name</td>
		 <td id='dept' title='Sort by Department' style="padding-bottom:2px;">
		 
	 	<select size='1' id='deptFilter' name='deptFilter' onChange="departmentFilter()">
		<option value='0'>All Departments</option>
		<?php	db_connect ();
		
			$query = "SELECT * FROM dlist ORDER BY department";
			$result = mysql_query ($query);
	
			while ($row = mysql_fetch_array($result)) 
			{
				if ($row[id] == $d)
					$selected = "selected";
				else
					$selected = "";
					
				echo "<option $selected value=\"$row[id]\">$row[department]</option>";
			}
		
			mysql_free_result ($result);
		?>	
		</select></td>
		 <td id='avg' title='Rating' style="padding-bottom:2px;">Rating</td>
		 </tr></thead>
		 <tbody id="tableBody">
	<?php
	db_connect ();
	
	if ($d)
		$dept = "WHERE departmentID=$d";
	else
		$dept = "";
	
	$query = "SELECT * FROM ilist " . $dept . " ORDER BY lName, fName";
	$iresult = mysql_query($query) 
		or die('Query failed: ' . mysql_error());
		
		
	// print the contents of the table
	while ($line = mysql_fetch_array($iresult))
	{
		// get the department name if not sorted by department
		if (d == 0)
		{
			$dept = $line['departmentID'];
			$query = "SELECT department FROM dlist WHERE id='$dept'";
			$dresult = mysql_query ($query)
				or die ('cannot access dlist '. mysql_error ());
			$department = mysql_result ($dresult, 0);
		}
		else
			$department = $departmentName;

		echo "<tr>
			  <td class='leftcol'><a href='read.php?id=$line[id]'>$line[lName], $line[fName]</a></td>
			  <td><a href='read.php?id=$line[id]'>$department</a></td>";
		if ($line[ratingTotal] == 0)
		{
			$formatted = "N/A";
		}
		else
		{
			$avg = $line[rating] / $line[ratingTotal];
			$formatted = sprintf("%.2f", $avg);
		}
		echo "<td class='rightcol'><a href='read.php?id=$line[id]'>$formatted</a></td>";
		echo "</tr>";
	}
	echo "</tbody>";
	echo "</table>";
?>



<?php
	
	do_footer ();
}
?>